package com.alibaba.moco.didi;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.moco.util.BaseTypeUtil;

import groovy.lang.GroovyObject;
import groovy.util.GroovyScriptEngine;

public class MocoScriptRunner {

	/**
	 * 
	 * @param request
	 * @param scriptName
	 * @return
	 */
	public static Object excuteScript(HttpServletRequest request, String scriptName) {
		try {
			System.out.println(BaseTypeUtil.getClassPath());
			GroovyScriptEngine engine = new GroovyScriptEngine(BaseTypeUtil.getClassPath() + "/script");
			Class<?> scriptClass = engine.loadScriptByName(scriptName);
			
			GroovyObject scriptInstance = (GroovyObject) scriptClass
					.newInstance();
			Object res = scriptInstance.invokeMethod("excute", request);
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
